﻿namespace ChinaUnicom.Views.Themes
{
    using Avatar.Themes;
    using Microsoft.Phone.Controls;
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;

    public partial class ThemePage : PhoneApplicationPage
    {
        public ThemePage()
        {
            InitializeComponent();

            Loaded += OnPageLoaded;
        }

        void OnPageLoaded(object sender, RoutedEventArgs e)
        {
            switch (ApplicationSettings.Settings.Theme)
            {
                case Theme.Light:
                    themePicker.SelectedIndex = 0;
                    break;
                case Theme.Dark:
                    themePicker.SelectedIndex = 1;
                    break;
                case Theme.System:
                    themePicker.SelectedIndex = 2;
                    break;
            }

            themePicker.SelectionChanged -= OnThemePickerSelectionChanged;
            themePicker.SelectionChanged += OnThemePickerSelectionChanged;
        }

        void OnThemePickerSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var picker = sender as ListPicker;
            if (picker != null)
            {
                switch (picker.SelectedIndex)
                {
                    case 0:
                        ApplicationSettings.Settings.Theme = Theme.Light;
                        break;
                    case 1:
                        ApplicationSettings.Settings.Theme = Theme.Dark;
                        break;
                    default:
                        ApplicationSettings.Settings.Theme = Theme.System;
                        break;
                }
            }
        }

        private void OnBackClick(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}